package university;
/**
 * Class to describe events such as classes tests and exams
 * @author anatolii.bondarenko
 */
import university.exceptions.*;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


class ExamEvent {
    private int building;
    private int room;
    private Date dateOfEvent;
    /**
     * All information about event
     * @param b is number of study building
     * @param r is number of auditorium
     * @param day is the day of event
     * @param month is month of event
     * @param year is year of event
     * @param hour is hour of start of event
     * @param minute is minutes of start of event
     */
    public ExamEvent(int b, int r, int year, int month, int day, int hour, int minute) throws InvalidDateException, InvalidBuildingException, InvalidRoomException{
		building = b;
		room = r;
        if (b < 1){
            throw new InvalidBuildingException("Building Number must be positive Integer");
        }
        if (r < 1){
            throw new InvalidRoomException("Room Number must be positive integer");
        }

        Calendar now = new GregorianCalendar().getInstance();
        if (year < now.get(Calendar.YEAR)){
            throw new InvalidDateException("Invalid year");
        }
        if (month < Calendar.JANUARY || month > Calendar.DECEMBER){
            throw new InvalidDateException("Invalid Month");
        }
        if (day < 1){
            throw new InvalidDateException("Invalid Day");
        }
        if(month == Calendar.FEBRUARY && (year % 4 == 0) && day > 29){
            throw new InvalidDateException("Invalid Day");
        }
        if(month == Calendar.FEBRUARY && (year % 4 != 0) && day > 28){
            throw new InvalidDateException("Invalid Day");
        }
        if((month == Calendar.APRIL || month == Calendar.JUNE || month == Calendar.SEPTEMBER || month == Calendar.NOVEMBER) && day > 30){
            throw new InvalidDateException("Invalid Day");
        }
        if ((month == Calendar.JANUARY || month == Calendar.MARCH || month == Calendar.MAY || month == Calendar.JULY || month == Calendar.AUGUST || month == Calendar.OCTOBER || month == Calendar.DECEMBER) && day > 31){
            throw new InvalidDateException("Invalid Day");
        }

        GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute);
        dateOfEvent = calendar.getTime();
	}

    /**
     * Gets time of event start
     * @return date and time of event start in standard Date format
     */
    public Date getEventDateAndTime(){
		return dateOfEvent;
	}

    /**
     * Gets number of auditorium
     * @return number of auditorium
     */
	public int getRoom(){
		return room;
	}

    /**
     * Gets number of study building
     * @return number of study building
     */
    public int getBuilding(){
		return building;
	}

    public String getEventTime(){
        return DateFormat.getTimeInstance().format(this.getEventDateAndTime());
    }

    public static void main(String[] args) throws Exception{
        File buildings = new File("buildings.dat");
        File rooms = new File ("rooms.dat");
        buildings.createNewFile();
        rooms.createNewFile();

        PrintWriter pw =new PrintWriter(buildings);
        for (int i=1; i<6; i++){
            pw.println(i);
        }
        pw.flush();
        pw.close();

        pw = new PrintWriter(rooms);
        for (Integer i=1; i <=5; i++){
            for (Integer j = 1; j <=25; j++){
                if (j>=10){
                    pw.println(i.toString()+j.toString());
                }else{
                    pw.println(i.toString()+"0"+j.toString());
                }
            }
        }
        pw.flush();
        pw.close();
    }
}